package com.example.controller;

import com.example.repository.read.City;
import com.example.repository.read.mappers.CityMapper;
import com.example.repository.write.Country;
import com.example.repository.write.CountryDto;
import com.example.repository.write.datatypes.Region;
import com.example.repository.write.mappers.CountryMapper;
import com.example.repository.write.mappers.ICity2Mapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hello")
public class hello
{
    private CityMapper _cityMapper;

    private CountryMapper _countryMapper;
    private ICity2Mapper _c2m;

    public hello(CityMapper cm, CountryMapper coma, ICity2Mapper c2m){
        this._cityMapper = cm;
        this._countryMapper = coma;
    }

    @GetMapping("/details/{state}")
    @Transactional(transactionManager = "hello11tm")
    public City getDarenDetials(@PathVariable("state") String state)
    {
        return _cityMapper.findByState(state);
    }

    @GetMapping("/country/{state}")
    public List<CountryDto> getDarenDetails(@PathVariable("state") String state){
        List<CountryDto> cys =  _countryMapper.findByState(state);
        for (CountryDto cc: cys
             )
        {
            System.out.println(cc.getNumber());
        }
        return cys;
    }
    @PostMapping("/country/add")
    @Transactional(transactionManager = "hello22tm")
    public void addCountry(@RequestBody BindModel bm){
        Country co = new Country();
        co.setNumber(Region.CHINA);
        co.setState(bm.state);
        _countryMapper.create(co);
        //_c2m.findByState("yyy");
    }

    public static class BindModel{
        private String state;
        private String number;

        public String getState()
        {
            return state;
        }

        public void setState(String state)
        {
            this.state = state;
        }

        public String getNumber()
        {
            return number;
        }

        public void setNumber(String number)
        {
            this.number = number;
        }
    }
}
